//
//  ParternersTableViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/11/21.
//  Copyright © 2018 xyxorigation. All rights reserved.
//

import UIKit

protocol ArrayHandle {
    func handleArray(array: Array<JSON>) -> Void
}

class ParternersTableViewController: UITableViewController {

    
    var source: [JSON] = []
    
    var selectedIndex: [Int] = []
    
    var delegate: ArrayHandle?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.estimatedRowHeight = 40
        self.tableView.rowHeight = 40
        self.navigationController?.navigationBar.tintColor = .white
        self.tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "UITableViewCell")
        let sure = UIBarButtonItem.init(title: "确定", style: .plain, target: self, action: #selector(sureAction))
        if self.navigationController != nil {
            if let index = self.navigationController?.children.firstIndex(of: self) {
                if index > 0 {
                    let back = UIBarButtonItem.init(image: UIImage.init(named: "icon_nav_whiteback")!.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(pop))
                    self.navigationItem.leftBarButtonItem = back
                } else {
                    self.navigationItem.leftBarButtonItem = nil
                }
            }
        }
        sure.tintColor = UIColor.white
        self.navigationItem.rightBarButtonItem = sure
        self.tableView.reloadData()
        self.tableView.tableFooterView = UIView()
    }
    
    @objc func sureAction()->Void{
        
        let array = selectedIndex.map { (index) -> JSON in
            if index < self.source.count {
                return self.source[index]
            } else {
                return JSON()
            }
        }
        self.delegate?.handleArray(array: array)
        self.pop()
        
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return source.count
    }

    
    override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 55
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 55
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell")!
        let item = source[indexPath.row]
        cell.selectionStyle = .none
        cell.textLabel?.text = item.contents?["name"]?.rawString()
        if self.selectedIndex.contains(indexPath.row) {
            cell.accessoryType = .checkmark
        } else {
            cell.accessoryType = .none
        }
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath)!
        let row = indexPath.row
        if selectedIndex.contains(row) {
            if let index = selectedIndex.firstIndex(of: row) {
                selectedIndex.remove(at: index)
                cell.accessoryType = .none
            }
        } else {
            selectedIndex.append(row)
            cell.accessoryType = .checkmark
        }
    }

}
